package com.tengju.bff.interfaces.shared.render;

import com.tengju.data.domain.model.analysis.Meta;
import com.tengju.data.domain.model.analysis.Row;
import com.tengju.data.domain.shared.CastUtil;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.function.Function;

/**
 * @author chenhang
 * @Date 2021/8/10
 */
public class PieAssembler {

    public static String PIE_NAME_KEY = "pieShowName";


    public static void buildData(PieVO pie,
                                 Meta meta,
                                 List<Row> rows,
                                 Function<Row,Row> preConsumer) {
        if (CollectionUtils.isEmpty(rows)) {
            return ;
        }
        rows.stream()
                .map(preConsumer)
                .forEach(row -> {
                    Object value = row.getValue(meta.getSqlColName());
                    Object key = row.getValue(PIE_NAME_KEY);
                    pie.add(CastUtil.castString(key),value);
                });

    }
}
